<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220309
 * Time: 11:05 
 * 统计及记录数据
 */
namespace app\home\model;

use app\home\model\Model;
use app\home\model\Redis;

class Record extends Model{

	private $counttable = 'web_count';

    /**
     * 统计访问量
     * @param array $count
     * @param array $where
     */
    public function countnum($key,$table='web_count',$keyname='title',$valname='count')
    {
        // if(readconfig("Redis")){
        //     $click = Redis::getInstance()->runCommand('incr',[$key]);
        //     return true;
        // }else{
            $filename = config('log.path') . 'count/'. $key.'/click_log.txt';  
             $s="";
             if (file_exists($filename)) {
             $content = file_get_contents($filename);
             $d_ary = array();
                if ($content) {
                  $ary = explode("/n", $content);
                    foreach ($ary as $line) {
                      $data_ary = explode('|', $line);

                      if (@is_numeric($data_ary[1])) {
                          $d_ary[$data_ary[0]] = $data_ary[1];
                       }
                    }
                }

                if (array_key_exists($key,$d_ary)){
                $d_ary[$key]=intval($d_ary[$key])+1; //将当前的点击数加1        
                } else {
                    $d_ary[$key]=1;
                }
                foreach($d_ary as $k=>$val){
                    $s.=$k.'|'.$val."/n";
                }
             }else{
                  mkdir(config('log.path') . 'count/'. $key, 0777, true); 
                  $s.=$key.'|1'."/n"; //这个是初始化记录文件   
             }  
            //写记录文件
            $this->filep($filename,$s,'w'); 
            //写更新时间文件
            $last_update = file_get_contents(config('log.path').'count/'.$key.'/last_update.txt');
            $last_update = intval($last_update);
            $this->filep(config('log.path') . 'count/'.$key.'/last_update.txt',time()); 

            //7200秒将数据存入数据库
            if (($last_update + 7200*6) < time()){
                $file = $this->countupdate($key,$d_ary[$key],$table,$keyname,$valname);
                if($file){
                    //清空数据
                    $this->filep($filename,'');
                    //重写更新时间
                    $this->filep(config('log.path'). 'count/'.$key.'/last_update.txt', time());                       
                }     
            }

        // }
        return true;
        
    }
    

    /**
     * 更新统计数
     * @param array $
     * @param array $where
     */
    private function countupdate($key,$count,$table='web_count',$keyname='title',$valname='count')
    {
        $res = Model::name($table)->field($valname)->where($keyname.'="'.$key.'"')->find();
        if($res){
           $data=array($valname=>$count+$res[$valname]);
           $count = Model::name($table)
                    ->where($keyname,'=',$key)
                    ->update($data); 
        }else{
           $data=array($keyname=>$key,$valname=>$count);
           $count = Model::name($table)
                    ->insert($data); 
        }
        unset($res);
        return $count;
    }

    /**
     * 书写文件
     * @param array $
     * @param array $where
     */
    private function filep($filename,$s,$w='w')
    {
        //写记录文件    
        $fp = fopen($filename, $w); //加入锁定
        if (flock($fp, LOCK_EX)){
        // 进行排它型锁定
            fwrite($fp, $s);
            flock($fp, LOCK_UN); // 释放锁定
        }
        fclose($fp);
        @chmod($filename, 0777); 
    }

}
